<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <title>订单管理</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/admin.css">
</head>
<body>
<div class="container">
    <h1>订单管理</h1>

    <c:if test="${not empty requestScope.success}">
        <div class="alert success">${requestScope.success}</div>
    </c:if>
    <c:if test="${not empty requestScope.error}">
        <div class="alert error">${requestScope.error}</div>
    </c:if>

    <table>
        <thead>
        <tr>
            <th>订单ID</th>
            <th>用户ID</th>
            <th>总价</th>
            <th>状态</th>
            <th>创建时间</th>
            <th>更新时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach items="${requestScope.orders}" var="order">
            <tr>
                <td>${order.orderId}</td>
                <td>${order.userId}</td>
                <td>¥<fmt:formatNumber value="${order.totalPrice}" pattern="#,##0.00"/></td>
                <td>${order.status}</td>
                <td><fmt:formatDate value="${order.createdAt}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
                <td><fmt:formatDate value="${order.updatedAt}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
                <td>
                    <c:if test="${order.status == '待发货'}">
                        <form action="${pageContext.request.contextPath}/admin/orders/update" method="post" style="display:inline;">
                            <input type="hidden" name="orderId" value="${order.orderId}">
                            <input type="hidden" name="status" value="已发货">
                            <button type="submit" class="btn">发货</button>
                        </form>
                        <form action="${pageContext.request.contextPath}/admin/orders/update" method="post" style="display:inline;">
                            <input type="hidden" name="orderId" value="${order.orderId}">
                            <input type="hidden" name="status" value="已取消">
                            <button type="submit" class="btn delete">取消</button>
                        </form>
                    </c:if>
                    <c:if test="${order.status != '待发货'}">
                        无操作
                    </c:if>
                </td>
            </tr>
            <!-- 订单项详情 -->
            <tr>
                <td colspan="7">
                    <table class="inner-table">
                        <thead>
                        <tr>
                            <th>商品ID</th>
                            <th>商品名称</th>
                            <th>单价</th>
                            <th>数量</th>
                            <th>小计</th>
                        </tr>
                        </thead>
                        <tbody>
                        <c:forEach items="${order.items}" var="item">
                            <tr>
                                <td>${item.productId}</td>
                                <td>${item.productName}</td>
                                <td>¥<fmt:formatNumber value="${item.price}" pattern="#,##0.00"/></td>
                                <td>${item.quantity}</td>
                                <td>¥<fmt:formatNumber value="${item.price.multiply(requestScope.java.math.BigDecimal.valueOf(item.quantity))}" pattern="#,##0.00"/></td>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</div>
</body>
</html>